#!/bin/bash

# 加载配置
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${SCRIPT_DIR}/config.sh"

# 检查权限和环境
check_root
check_install_env

# 从services.json获取服务列表
SERVICES=$(jq -r '.start_order[]' services.json)

# 启动所有微服务
info "开始启动微服务..."
total=$(echo "$SERVICES" | wc -w)
current=1

for service in $SERVICES; do
    if [ -x "$INSTALL_FULL_PATH/cmd/${service}/bin/${service}" ]; then
        info "[$current/$total] 启动微服务: ${service}"
        
        # 确保日志目录存在
        mkdir -p "$FDSMASTERGO_LOG_DIR/${service}"
        
        # 使用nohup启动服务
        cd "$FDSMASTERGO_HOME" && \
        nohup "$INSTALL_FULL_PATH/cmd/${service}/bin/${service}" >/dev/null 2>&1 &
        
        # 从services.json获取延迟时间
        delay=$(jq -r ".services.${service}.delay" services.json)
        #info "[$current/$total] 等待 ${delay} 秒..."
        sleep "$delay"

        # 查看这个服务的状态
        if pgrep -x "$service" > /dev/null; then
            info "[$current/$total] ${service} 启动成功"
        else
            warn "[$current/$total] ${service} 启动失败"
        fi
    else
        warn "[$current/$total] 微服务不可执行: $INSTALL_FULL_PATH/cmd/${service}/bin/${service}"
    fi
    current=$((current + 1))
done

info "所有微服务启动完成" 